﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using RM2012Server.Business;

namespace RM2012Server.Model
{
    public class UnitDAO
    {
        DataServices m_UnitDAO = new DataServices();

        public DataTable GetAll()
        {
            m_UnitDAO.Load("GetAllUnit", null);
            return m_UnitDAO;
        }

        public DataTable GetById(String unitId)
        {
            SqlParameter param = new SqlParameter("@UnitId", SqlDbType.VarChar, 6);
            param.Value = unitId;

            m_UnitDAO.Load("GetUnitById", param);
            return m_UnitDAO;
        }

        public DataTable GetByLikeId(String unitId)
        {
            SqlParameter param = new SqlParameter("@UnitId", SqlDbType.VarChar, 6);
            param.Value = unitId;

            m_UnitDAO.Load("GetUnitByLikeId", param);
            return m_UnitDAO;
        }

        public DataTable GetByName(String stateName)
        {
            SqlParameter param = new SqlParameter("@UnitName", SqlDbType.NVarChar, 50);
            param.Value = stateName;

            m_UnitDAO.Load("GetUnitByName", param);
            return m_UnitDAO;
        }

        public int Add(UnitInfo info)
        {
            SqlParameter[] param = new SqlParameter[3];
            param[0] = new SqlParameter("@UnitId", SqlDbType.VarChar, 6);
            param[0].Value = info.UnitId;

            param[1] = new SqlParameter("@UnitName", SqlDbType.NVarChar, 50);
            param[1].Value = info.UnitName;

            param[2] = new SqlParameter("@Remark", SqlDbType.NVarChar, 500);
            param[2].Value = info.Remark;

            return m_UnitDAO.ExecuteUpdate("AddUnit", param);
        }

        public int Update(UnitInfo info)
        {
            SqlParameter[] param = new SqlParameter[3];
            param[0] = new SqlParameter("@UnitId", SqlDbType.VarChar, 6);
            param[0].Value = info.UnitId;

            param[1] = new SqlParameter("@UnitName", SqlDbType.NVarChar, 50);
            param[1].Value = info.UnitName;

            param[2] = new SqlParameter("@Remark", SqlDbType.NVarChar, 500);
            param[2].Value = info.Remark;

            return m_UnitDAO.ExecuteUpdate("UpdateUnit", param);
        }

        public int Delete(String unitId)
        {
            SqlParameter param = new SqlParameter("@UnitId", SqlDbType.VarChar, 6);
            param.Value = unitId;

            return m_UnitDAO.ExecuteNoneQuery("DeleteUnit", param);
        }

        public DataRow AddNewRow()
        {
            return m_UnitDAO.NewRow();
        }

        public void AddNewUnit(DataRow m_Row)
        {
            m_UnitDAO.Rows.Add(m_Row);
        }

        public bool SaveData()
        {
            return m_UnitDAO.ExecuteNoneQuery() > 0;
        }
    }
}
